import 'package:flustars/flustars.dart';
import 'dart:async';
import '../base/user.dart';
import 'dart:convert';
import 'package:drm/base/appurl.dart';
class SpUtils {

  //获取本地User的数据；
  static User getLocalUser() {

    String userJsonStr = SpUtil.getString('user') ;
    print('获取本地user = $userJsonStr');
    User user ;
    if(userJsonStr.isNotEmpty) {
      try {
        user = User.fromJson(json.decode(userJsonStr) );
      }catch(e) {
        print(e.toString());
        user = null;
      }
    }
    return user;
  }

  //保存本地User数据；
  static Future<bool> saveLocalUser(User user) {
    String json = jsonEncode(user.toJson());
    print('保存本地User'+json);
    return SpUtil.putString('user', json);
  }



  //获取本地服务器数据；
  static AppIpPort getAppUrl() {

    String userJsonStr = SpUtil.getString('appUrl') ;

    AppIpPort appUrl ;
    if(userJsonStr.isNotEmpty) {
      try {
        appUrl = AppIpPort.fromJson(json.decode(userJsonStr) );
      }catch(e) {
        print(e.toString());
        appUrl = null;
      }
    }
    return appUrl;
  }

  //保存本地服务器数据；
  static Future<bool> saveAppUrl(AppIpPort appUrl) {
    return SpUtil.putString('appUrl', jsonEncode(appUrl.toJson()));
  }


  static String getBaseUrl() {
    AppIpPort appUrl = getAppUrl() ;
    if (appUrl== null || appUrl.ip.isEmpty || appUrl.port.isEmpty) {
//      return 'http://222.188.209.243:82/';
    return '';
    }else {
      return 'http://${appUrl.ip}:${appUrl.port}/' ;
    }
  }

}